<?php
/**
 * Created by PhpStorm.
 * User: shenyvlin
 * Date: 2019/2/27
 * Time: 15:56
 */
namespace app\api\model\user;

use think\Model;
use think\Db;
class Membermodel extends Model{
    /**
     * @param $data
     * @return int 1 注册成功 2 已注册 3 注册失败 4 已注册未编辑基本信息
     */
    public function registerUser($data){
        $user = Db::name('users')->where('mobile',$data['mobile'])->find();
        if($user){
            if($user['realname'] && $user['birthday']){
                return 2; // 已注册
            }else{
                return 4; // 已注册未编辑基本信息
            }
        }else{
            $user_level = db('user_level')->find();
            $data['level'] = $user_level['level_id'];
            $res = Db::name('users')->insertGetId($data);
            if($res){
                return $res;
            }else{
                return 3; // 注册失败
            }
        }
    }


    /**
     * @param $data
     * @return int 1 登录成功 2 用户未注册 3 密码错误
     */
    public function loginUser($data){
        $where = [
            'mobile' => $data['mobile'],
            'del' => 0
        ];
        $user = Db::name('users')->where($where)->find();
        return $user;
    }
    
    // 基础信息 - 注册第二步
    public function regBase($data){
        $info = [
            'realname' => $data['realname'],
            'birthday' => $data['birthday'],
            'address' => $data['address'],
            'email' => $data['email']
        ];
        $res = Db::name('users')->where('id',$data['uid'])->update($info);
        if($res){
            // 注册成功，session存储用户uid
            $userInfoInfo = Db::name('users')->where('id',$data['uid'])->find();
            $uid = $userInfoInfo['id'];
            $mobile = $userInfoInfo['mobile'];
            session('uid',$uid);
            session('mobile',$mobile);
            return $uid;
        }else{
            return 2;
        }
    }

    public function updateUserInfo($data){
        $info = [
            'sex' => $data['sex'],
            'idcard' => $data['idcard'],
            'mobile' => $data['mobile'],
            'realname' => $data['realname']
        ];
        $res = db('users')->where('id',$data['user_id'])->update($info);
        if($res){
            return true;
        }else{
            return false;
        }
    }

    // 基础信息
    public function setBase($data){
        $info = [
            'sex' => $data['sex'],
            'photo' => $data['photo_url'],
            'nickname' => $data['nickname'],
            'real_name' => $data['real_name'],
            'birthday' => $data['birthday'],
            'constellation' => $data['constellation'],
            'height' => $data['height'],
            'monthly' => $data['monthly'],
            'live_province' => $data['live_province'],
            'live_city' => $data['live_city'],
            'live_county' => $data['live_county'],
            'home_province' => $data['home_province'],
            'home_city' => $data['home_city'],
            'home_county' => $data['home_county'],
            'marry_time' => $data['marry_time'],
            'base_info_time' => date("Y-m-d H:i:s",time()),
        ];
        $is_have = db('users')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = Db::name('lj_users_baseinfo')->where('uid',$data['uid'])->update($info);
        }else{
            $info['uid'] = $data['uid'];
            $res = Db::name('lj_users_baseinfo')->insert($info);
            db('lj_users')->where('id',$data['uid'])->update(['user_id'=>$data['user_id']]);
        }
        if($res){
            return 1;
        }else{
            return 2;
        }
    }
    // 个人介绍
    public function baseUserIntroduction($data){
        $info = [
            'ntroduction' => $data['ntroduction']
        ];
        $res = Db::name('lj_users_baseinfo')->where('uid',$data['uid'])->update($info);
        if($res){
            return 1;
        }else{
            return 2;
        }
    }

    //获取用户默认地址
    public function getUserDefaultAddress($uid){
        $address = db('users_address')->where(['uid'=>$uid, 'default'=>1])->find();
        if($address){
            $region = db('region')->where(['id'=>[$address['province'], $address['city'], $address['district'] ]])->select();
            $addressName = [];
            foreach($region as $val){
                $addressName[$val['id']] = $val['name'];
            }
            $address['address'] = $addressName[$address['province']].$addressName[$address['city']].$addressName[$address['district']].$address['address'];
        }
        return $address;
    }

    //获取用户默认地址
    public static function getAddressInfo($id){
        $address = db('users_address')->where(['id'=>$id])->find();
        if($address){
            $region = db('region')->where(['id'=>[$address['province'], $address['city'], $address['district'] ]])->select();
            $addressName = [];
            foreach($region as $val){
                $addressName[$val['id']] = $val['name'];
            }
            $address['address'] = $addressName[$address['province']].$addressName[$address['city']].$addressName[$address['district']].$address['address'];
        }
        return $address;
    }

    /**
     * 个人卖家入驻
     */
    public function saleUserRegModel($data){
        $uid = $data['uid'];
        unset($data['uid']);
        $data['type'] = 2;
        $data['sale_time'] = date("Y-m-d H:i:s",time());
        $res = db('users')->where('id',$uid)->update($data);
        if($res){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 查询是否存在未支付认证订单
     */
    public function selectOrder($pid,$uid){
        $res = Db::name('lj_order')->where([
            'order_status' => 0,
            'uid' => $uid,
            'product_id' => $pid
        ])->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    /**
     * 创建认证订单
     * @return bool
     */
    public function createAuthOrder($data){
        $res = Db::name('lj_order')->insertGetId($data);
        if($res){
            $info = db('lj_order')->where('id',$res)->find();
            return $info;
        }else{
            return false;
        }
    }

    /**
     * 获取认证商品信息
     * @return array|bool|null|\PDOStatement|string|Model
     */
    public function getAuthProduct(){
        $res = db('lj_product')->where('product_flag','auth')->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function getGoldProduct(){
        $where = [
            'product_flag' => 'gold',
            'del' => 0
        ];
        $res = db('lj_product')->where($where)->select();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    /**
     * 检测登录
     * @return bool
     */
    public function checkLogin(){
        $uid = $this->user['id'];
        $mobile = session('mobile');
        if($uid && $mobile){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 查询邀请码真伪
     */
    public function checkInvitationCode($code){
        $res = db('lj_users')->where('invitation_code',$code)->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    /**
     * 填写邀请码
     */
    public function writeInvitationCode($code_uid,$uid){
        $data = [
            'invitation_user'=>$code_uid,
            'auth' => 1
            ];
        $res = db('lj_users')->where('id',$uid)->update($data);
        if($res){
            return true;
        }else{
            return false;
        }
    }

    // 保存/更新 用户微信信息
    public function saveWxQr($data){
        $is_have = db('lj_users_wxqr')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = db('lj_users_wxqr')->where('uid',$data['uid'])->update($data);
        }else{
            $res = db('lj_users_wxqr')->insert($data);
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 根据id查询用户微信
     */
    public function getUserWxQrInfo($uid){
        $res = db('lj_users_wxqr')->where('uid',$uid)->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    // 提交微信审核
    public function getWxQrVerify($uid){
        $data = [
            'status' => 1,
            'verify_time' => date('Y-m-d H:i:s',time())
        ];
        $res = db('lj_users_wxqr')->where('uid',$uid)->update($data);
        if($res){
            return $res;
        }else{
            return false;
        }
    }
    // 提交微信上架
    public function getWxQrUp($data){
        $info = ['is_up'=>$data['type']];
        $res = db('lj_users_wxqr')->where('uid',$data['uid'])->update($info);
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function saveWorkInfo($data){
        $is_have = db('lj_users_work')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = db('lj_users_work')->where('uid',$data['uid'])->update($data);
        }else{
            $res = db('lj_users_work')->insert($data);
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }

    public function getUserWorkInfo($uid){
        $res = db('lj_users_work')->where('uid',$uid)->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    // 提交职业审核
    public function getUserWorkVerifyInfo($uid){
        $data = [
            'status' => 1,
            'verify_time' => date('Y-m-d H:i:s',time())
        ];
        $res = db('lj_users_work')->where('uid',$uid)->update($data);
        if($res){
            return $res;
        }else{
            return false;
        }
    }
    public function getEducation(){
        $res = db('lj_education')->select();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function saveEduInfo($data){
        $is_have = db('lj_users_edu')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = db('lj_users_edu')->where('uid',$data['uid'])->update($data);
        }else{
            $res = db('lj_users_edu')->insert($data);
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }

    public function getUserEduInfo($uid){
        $res = db('lj_users_edu')->alias('e')
            ->field('e.*,edu.name as edu_name')
            ->where('e.uid',$uid)
            ->join('lj_education edu','edu.id = e.edu_id')
            ->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function getUserEduVerifyInfo($uid){
        $data = [
            'status' => 1,
            'verify_time' => date('Y-m-d H:i:s',time())
        ];
        $res = db('lj_users_edu')->where('uid',$uid)->update($data);
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function saveCarInfo($data){
        $is_have = db('lj_users_car')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = db('lj_users_car')->where('uid',$data['uid'])->update($data);
        }else{
            $res = db('lj_users_car')->insert($data);
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }

    public function getUserCarInfo($uid){
        $res = db('lj_users_car')->where('uid',$uid)->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function getUserCarVerifyInfo($uid){
        $data = [
            'status' => 1,
            'verify_time' => date('Y-m-d H:i:s',time())
        ];
        $res = db('lj_users_car')->where('uid',$uid)->update($data);
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function saveHouseInfo($data){
        $is_have = db('lj_users_house')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = db('lj_users_house')->where('uid',$data['uid'])->update($data);
        }else{
            $res = db('lj_users_house')->insert($data);
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }

    public function getUserHouseInfo($uid){
        $res = db('lj_users_house')
            ->alias('h')
            ->field('h.*,p.areaName province_name,c.areaName city_name,t.areaName county_name')
            ->where('h.uid',$uid)
            ->join('lj_china p','p.areaId = h.province')
            ->join('lj_china c','c.areaId = h.city')
            ->join('lj_china t','t.areaId = h.county')
            ->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function getUserHouseVerifyInfo($uid){
        $data = [
            'status' => 1,
            'verify_time' => date('Y-m-d H:i:s',time())
        ];
        $res = db('lj_users_house')->where('uid',$uid)->update($data);
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function saveMarryInfo($data){
        $is_have = db('lj_users_marry')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = db('lj_users_marry')->where('uid',$data['uid'])->update($data);
        }else{
            $res = db('lj_users_marry')->insert($data);
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }
}